package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.mapper.TbCoachMapper;
import com.maiworld.mapper.TbEvaluationMapper;
import com.maiworld.mapper.TbReservationMapper;
import com.maiworld.mapper.TbStudentMapper;
import com.maiworld.pojo.TbCoach;
import com.maiworld.pojo.TbEvaluation;
import com.maiworld.pojo.TbEvaluationExample;
import com.maiworld.pojo.TbEvaluationExample.Criteria;
import com.maiworld.pojo.TbReservation;
import com.maiworld.pojo.TbStudent;
import com.maiworld.seller.service.EvaluationService;

import entity.PageResult;

@Service
public class EvaluationServiceImpl implements EvaluationService {

	@Autowired
	private TbEvaluationMapper tbEvaluationMapper;
	
	@Autowired
	private TbCoachMapper tbCoachMapper;
	
	@Autowired
	private TbStudentMapper tbStudentMapper;
	
	@Autowired
	private TbReservationMapper tbReservationMapper;
	
	@Override
	public List<TbEvaluation> findAll() {
		
		return tbEvaluationMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<TbEvaluation> page = (Page<TbEvaluation>) tbEvaluationMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbEvaluation evaluation) {
		evaluation.setCommentee("0");
		evaluation.setCommentee("1");
		tbEvaluationMapper.insert(evaluation);

	}

	@Override
	public void update(TbEvaluation evaluation) {
		tbEvaluationMapper.updateByPrimaryKey(evaluation);

	}

	@Override
	public TbEvaluation findOne(Long id) {
		
		return tbEvaluationMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			 tbEvaluationMapper.deleteByPrimaryKey(id);
		}

	}

	@Override
	public PageResult findPageEvaluation(TbEvaluation evaluation, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		TbEvaluationExample example = new TbEvaluationExample();
		Criteria create = example.createCriteria();
			
			if(evaluation!=null){
				if(evaluation.getContent()!=null && evaluation.getContent().length()>0){
					create.andContentLike("%"+evaluation.getContent()+"%");
				}if(evaluation.getStatus()!=null && evaluation.getStatus().length()>0){
					create.andStatusEqualTo(evaluation.getStatus());
				}if(evaluation.getCommentee()!=null && evaluation.getCommentee().length()>0){
					create.andCommenteeLike("%"+evaluation.getCommentee()+"%");
				}
			}
		Page<TbEvaluation> page = (Page<TbEvaluation>) tbEvaluationMapper.selectByExample(example );
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public PageResult selectByevaluationList(TbEvaluation evaluation,int pageNum, int pageSize) {

		PageHelper.startPage(pageNum, pageSize);
		List<TbEvaluation> evaluationList = tbEvaluationMapper.selectByevaluationList(evaluation);

		Page<TbEvaluation> page = (Page<TbEvaluation>) evaluationList;
		return new PageResult(page.getTotal(), page.getResult());
	}
	
	//教练的详细信息
	@Override
	public Map<String, Object> findEva( String openid,Long sid,Long cid,String status) {
		Map<String, Object> result = new HashMap<String,Object>();
		
		TbCoach coach = new TbCoach();
		coach = tbCoachMapper.selectByPrimaryKey(cid);
		result.put("coach_detail", coach);
		
		List<TbEvaluation> array = new ArrayList<>();
		TbEvaluation example = new TbEvaluation();
		example.setCid(cid);
		example.setSid(sid);
		array = tbEvaluationMapper.selectByEvaluCid(example);
		
		List<TbEvaluation> arrayList = new ArrayList<>();
		for (TbEvaluation a : array) {
			TbStudent b = tbStudentMapper.selectByPrimaryKey(a.getSid());
			a.setSname(b.getNickName());
			a.setHeadPic(b.getHeadPic());
			arrayList.add(a);
		}
		
		result.put("retes", arrayList);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	}

	//用户提交评论
	@Override
	public Map<String, Object> addEvaluation(Map<String, String> params) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		TbEvaluation a = new TbEvaluation();
		a.setCid(Long.parseLong(params.get("cid")));
		a.setSid(Long.parseLong(params.get("sid")));   
		a.setStar(Integer.parseInt(params.get("star")));   //评论几颗星级
		a.setContent(params.get("content"));				//评论内容
		a.setStatus(params.get("status"));					//所属分校
		a.setCommentee(params.get("commentee"));			//评论对象	
		a.setCreated(new Date());
		tbEvaluationMapper.insert(a);
		
		
		
		TbReservation rese = tbReservationMapper.selectByPrimaryKey(Long.parseLong(params.get("id")));	
		rese.setCheckStatus("4");
		tbReservationMapper.updateByPrimaryKey(rese);
		
		TbCoach coach = tbCoachMapper.selectByPrimaryKey(Long.parseLong(params.get("cid")));
		int b = coach.getStar();  //教练的星级
		int c = a.getStar();	//学员评价的数字
		int d = 5;
		int e = 4;
		int f = b*e;
		int g = f + c;		
		int h = (int) Math.floor(g/e);
		coach.setStar(h);
		tbCoachMapper.updateByPrimaryKey(coach);
		result.put("statusCode", 200);
		result.put("message", "提交成功");
		return result;
	}
	
	
	

	

}
